package com.westa.smallshop.goodsadd;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseActivity;
import com.westa.smallshop.common.BaseFragment;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.util.ListUtils;
import com.westa.smallshop.widget.ImageGridView;

import butterknife.BindString;
import butterknife.BindView;
import io.valuesfeng.picker.ImageGridViewManage;

import static com.google.common.base.Preconditions.checkNotNull;


public class GoodsAddFragment extends BaseFragment implements GoodsAddContract.View {

    public ImageGridViewManage mImageGridViewManage;
    @BindView(R.id.igv_image_grid_view)
    ImageGridView igvImageGridView;
    @BindView(R.id.et_goods_add_goods_name)
    EditText mEtGoodsAddGoodsName;
    @BindView(R.id.et_goods_add_goods_desc)
    EditText mEtGoodsAddGoodsDesc;
    @BindView(R.id.et_goods_add_goods_price)
    EditText mEtGoodsAddGoodsPrice;
    @NonNull
    private GoodsAddContract.Presenter mPresenter;

    @BindString(R.string.title_goods_add)
    String mTitle;

    public static GoodsAddFragment newInstance() {
        return new GoodsAddFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public int getCreateViewLayoutRes() {
        return R.layout.fragment_goods_add;
    }

    @Override
    public void initView() {
        getActivity().setTitle(mTitle);
        if (mImageGridViewManage == null) {
            mImageGridViewManage = new ImageGridViewManage(igvImageGridView, this, 1);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.destroy();
        if (mImageGridViewManage != null) {
            mImageGridViewManage.destroy();
        }
    }

    @Override
    public void showAddGoodsDialogProgress() {

        showDialogProgress(R.string.general_uploading_hint,
                new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mPresenter.cancelAddGoodsImage();
                    }
                });
    }

    @Override
    public void showAddGoodsSuccessful() {

        showSingleToast(R.string.general_upload_successful);
        getActivity().setResult(BaseActivity.RESULT_OK_REFRESH);
        getActivity().finish();
    }

    @Override
    public void showUploadImageFailed() {

        showSingleToast(R.string.general_upload_image_failed);
    }

    @Override
    public void showAddGoodsFailed() {
        showSingleToast(R.string.tst_upload_goods_failed);
    }

    @Override
    public void showGoodsNameEmpty() {
        showSingleToast(R.string.tst_add_goods_name_empty);
    }

    @Override
    public void showGoodsPriceEmpty() {
        showSingleToast(R.string.tst_add_goods_price_empty);
    }

    @Override
    public void showGoodsImgEmpty() {
        showSingleToast(R.string.tst_add_goods_img_empty);
    }

    @Override
    public void failedOperation(String msg) {

        showSingleToast(msg);
    }

    @Override
    public void showExitHintDialog() {
        if (!TextUtils.isEmpty(mEtGoodsAddGoodsDesc.getText().toString()) ||
                !ListUtils.isEmpty(mImageGridViewManage.getImages())) {

            new MaterialDialog.Builder(getActivity()).content(R.string.dlg_edit_exit_hint)
                    .positiveText(R.string.general_confirm)
                    .positiveColor(Color.RED)
                    .negativeText(R.string.general_cancel)
                    .negativeColor(Color.GRAY)
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            getActivity().finish();
                        }
                    })
                    .show();
        } else {
            getActivity().finish();
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ImageGridViewManage.REQUEST_CODE_CHOOSE &&
                resultCode == Activity.RESULT_OK && data != null) {
            if (mImageGridViewManage != null) {
                mImageGridViewManage.setOnActivityResult(data);
            }
        }
    }


    @Override
    public void setPresenter(@NonNull GoodsAddContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }


    @Override
    public void showGoodsDescriptionNull() {
        showSingleToast(R.string.tst_add_goods_desc_empty);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.goods_add_menu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                if (ListUtils.isEmpty(mImageGridViewManage.getImages())){
                    showGoodsImgEmpty();
                    break;
                }
                if (TextUtils.isEmpty(mEtGoodsAddGoodsPrice.getText().toString())){
                    showGoodsPriceEmpty();
                    break;
                }
                GoodsItem goodsItem = new GoodsItem();
                goodsItem.setGoods_name(mEtGoodsAddGoodsName.getText().toString());
                goodsItem.setGoods_desc(mEtGoodsAddGoodsDesc.getText().toString());
                goodsItem.setGoods_price(Integer.valueOf(mEtGoodsAddGoodsPrice.getText().toString()));
                mPresenter.doSaveGoods(mImageGridViewManage.getImages(), goodsItem);
                break;
        }
        return true;
    }

}
